//
//  ModuleDbListVC.m
//  Project
//
//  Created by 冯东辉 on 2018/9/27.
//  Copyright © 2018年 FengDongHui. All rights reserved.
//

#import "ModuleDbListVC.h"
#import "MJRefresh.h"
#import "CollectCell.h"
#import "CollectHeadView.h"
#import "ModuleListVC.h"
#import "PieView.h"
#import "ModuleDetailVC.h"
#import "CityDetailVC.h"

@interface ModuleDbListVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *data;

@property (nonatomic, strong) UIView *baseV;

@property (nonatomic, strong) NSMutableArray *dataItems;
@property (nonatomic, strong) NSMutableArray *colorItems;

@end

@implementation ModuleDbListVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.navTitleL.text = @"短板指标";
    [self createBlueNavigationBar:YES];
        
    [self initSubViews];
    
    [self loadNewData];
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}

#pragma mark - init
- (void)initSubViews {
    
    _baseV = [[UIView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight+1, GET_SCREEN_WIDTH(), GET_SCREEN_WIDTH()/2.0f)];
    [_baseV.layer addSublayer:[ProjectTools setDbModuleListJianBianWithBenginColor:UICOLOR_HEX(0xff299bf6) endColor:UICOLOR_HEX(0xff9579e3) frame:_baseV.bounds]];
    [self.view addSubview:_baseV];
    
    [self.view addSubview:self.tableView];
}

- (void)crearHeadView {
    
    _dataItems = [[NSMutableArray alloc] init];
    _colorItems = [[NSMutableArray alloc] init];
    
    NSInteger allCount = 0;
    
    for (int i = 0; i < [_data count]; i++) {
        
        NSDictionary *dic = [_data objectAtIndex:i];
        
        NSString *dimName = dic[@"dim_name"];
        
        NSInteger item = [dic[@"list"] count];
        
        allCount += item;
        
        [_dataItems addObject:@(item)];
        
        // 业务发展  无线接入  无线覆盖  无线语音业务  无线数据业务  无线网络结构  基站退服  宽带  魔盒
        if ([dimName isEqualToString:@"业务发展"]) {
            
            [_colorItems addObject:UICOLOR_HEX(0xfff2a356)];
            
        }else if ([dimName isEqualToString:@"无线接入"]) {
            
            [_colorItems addObject:UICOLOR_HEX(0xff6341f9)];
            
        }else if ([dimName isEqualToString:@"无线覆盖"]) {
            
            [_colorItems addObject:UICOLOR_HEX(0xff896af8)];
            
        }else if ([dimName isEqualToString:@"无限语音业务"]) {
            
            [_colorItems addObject:UICOLOR_HEX(0xff63e09d)];
            
        }else if ([dimName isEqualToString:@"无线数据业务"]) {
            
            [_colorItems addObject:UICOLOR_HEX(0xff20befc)];
            
        }else if ([dimName isEqualToString:@"无线网络结构"]) {
            
            [_colorItems addObject:UICOLOR_HEX(0xffa663d4)];
            
        }else if ([dimName isEqualToString:@"基站退服"]) {
            
            [_colorItems addObject:UICOLOR_HEX(0xffe270ab)];
            
        }else if ([dimName isEqualToString:@"宽带业务"]) {
            
            [_colorItems addObject:UICOLOR_HEX(0xfffc7e6a)];
            
        }else if ([dimName isEqualToString:@"传输网络"]) {
            
            [_colorItems addObject:UICOLOR_HEX(0xfff4a24f)];
            
        }else if ([dimName isEqualToString:@"魔百盒业务"]) {
            
            [_colorItems addObject:UICOLOR_HEX(0xff25bdab)];
            
        }else { // 实时数据
            
            [_colorItems addObject:UICOLOR_HEX(0xff9976e5)];
        }
    }
    
    CGFloat peiH = _baseV.height-15-10-10-15;
    
    PieView *pie = [[PieView alloc] initWithFrame:CGRectMake(15, 15, peiH, peiH) dataItems:_dataItems colorItems:_colorItems];
    [_baseV addSubview:pie];
    
    [pie stroke];
    
    UILabel *countL = [[UILabel alloc] initWithFrame:CGRectMake(0, pie.bottom, pie.width+30, 35)];
    countL.text = [NSString stringWithFormat:@"%@发现短板指标%ld个", _cityName, allCount];
    countL.textColor = UICOLOR_HEX(0xffffffff);
    countL.textAlignment = NSTextAlignmentCenter;
    countL.font = [UIFont systemFontOfSize:13];
    countL.numberOfLines = 0;
    [_baseV addSubview:countL];
    
    CGFloat leftH = [_data count]*30.0f;
    
    if (leftH > _baseV.height-20) {
        
        leftH = _baseV.height-20;
    }
    
    UIScrollView *leftV = [[UIScrollView alloc] initWithFrame:CGRectMake((GET_SCREEN_WIDTH()/2.0f+10), (_baseV.height-leftH)/2.0f, GET_SCREEN_WIDTH()/2.0f-20, leftH)];
    leftV.contentSize = CGSizeMake(leftV.width, [_data count]*30.0f);
    [_baseV addSubview:leftV];
    
    for (int i = 0; i < [_data count]; i++) {
        
        UIView *listV = [[UIView alloc] initWithFrame:CGRectMake(0, 30*i, leftV.width, 30)];
        [leftV addSubview:listV];
        
        UILabel *leftL = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, listV.width-10-47, listV.height)];
        leftL.textColor = UICOLOR_HEX(0xffffffff);
        leftL.font = [UIFont systemFontOfSize:13];
        leftL.textAlignment = NSTextAlignmentLeft;
        [listV addSubview:leftL];
        
        UILabel *rightL = [[UILabel alloc] initWithFrame:CGRectMake(leftL.right, 0, 47, listV.height)];
        rightL.textColor = UICOLOR_HEX(0xffffffff);
        rightL.font = [UIFont systemFontOfSize:13];
        rightL.textAlignment = NSTextAlignmentRight;
        [listV addSubview:rightL];
        
        NSDictionary *dic = [_data objectAtIndex:i];
        
        NSString *dimName = dic[@"dim_name"];
        
        NSInteger item = [dic[@"list"] count];
        
        leftL.text = dimName;
        
        rightL.text = [NSString stringWithFormat:@"%.2f%@", 100.0f/allCount*item, @"%"];
        
        // 业务发展  无线接入  无线覆盖  无线语音业务  无线数据业务  无线网络结构  基站退服  宽带  魔盒
        if ([dimName isEqualToString:@"业务发展"]) {
            
            listV.backgroundColor = UICOLOR_HEX(0xfff2a356);
            
        }else if ([dimName isEqualToString:@"无线接入"]) {
            
            listV.backgroundColor = UICOLOR_HEX(0xff6341f9);
            
        }else if ([dimName isEqualToString:@"无线覆盖"]) {
            
            listV.backgroundColor = UICOLOR_HEX(0xff896af8);
            
        }else if ([dimName isEqualToString:@"无限语音业务"]) {
            
            listV.backgroundColor = UICOLOR_HEX(0xff63e09d);
            
        }else if ([dimName isEqualToString:@"无线数据业务"]) {
            
            listV.backgroundColor = UICOLOR_HEX(0xff20befc);
            
        }else if ([dimName isEqualToString:@"无线网络结构"]) {
            
            listV.backgroundColor = UICOLOR_HEX(0xffa663d4);
            
        }else if ([dimName isEqualToString:@"基站退服"]) {
            
            listV.backgroundColor = UICOLOR_HEX(0xffe270ab);
            
        }else if ([dimName isEqualToString:@"宽带业务"]) {
            
            listV.backgroundColor = UICOLOR_HEX(0xfffc7e6a);
            
        }else if ([dimName isEqualToString:@"传输网络"]) {
            
            listV.backgroundColor = UICOLOR_HEX(0xfff4a24f);
            
        }else if ([dimName isEqualToString:@"魔百盒业务"]) {
            
            listV.backgroundColor = UICOLOR_HEX(0xff25bdab);
            
        }else { // 实时数据
            
            listV.backgroundColor = UICOLOR_HEX(0xff9976e5);
        }
    }
}

- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, _baseV.bottom, GET_SCREEN_WIDTH()-20, GET_SCREEN_HEIGHT()-_baseV.bottom) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

- (void)loadNewData {
    
    [[ProjectApi instance] getDbModuleListDataWithCityName:_cityName callBack:^(NetModel *object) {
        
        [self.tableView.mj_header endRefreshing];
        
        if (object.isSuccessed) {
            
            self.data = [NSMutableArray arrayWithArray:object.service];
            
            [self.tableView reloadData];
            
            [self crearHeadView];
            
        }else {
            
            [SVProgressHUD showErrorWithStatus:@"网络连接失败"];
            [SVProgressHUD dismissWithDelay:1.5f];
        }
    }];
}


#pragma mark - tableView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 1;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    static NSString *identifier_head = @"collect_head_view";
    
    CollectHeadView *headV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identifier_head];
    
    if (headV == nil) {
        
        headV = [[CollectHeadView alloc] initWithReuseIdentifier:identifier_head];
    }
    
    NSDictionary *dic = _data[section];
    
    NSString *dimName = [NSString stringWithFormat:@"%@", dic[@"dim_name"]];
    
    headV.iconV.image = [UIImage imageNamed:[NSString stringWithFormat:@"collect_%@", dimName]];
    headV.titleL.text = [NSString stringWithFormat:@"%@（%@）", dimName, dic[@"dimcount"]];
    
    headV.colorBaseBtn.tag = 999+section;
    [headV.colorBaseBtn addTarget:self action:@selector(sectionModuleClick:) forControlEvents:UIControlEventTouchUpInside];
    
    for (id layer in headV.colorBaseBtn.layer.sublayers) {
        
        if ([layer isKindOfClass:[CAGradientLayer class]]) {
            [layer removeFromSuperlayer];
            break;
        }
    }
    
    // 业务发展  无线接入  无线覆盖  无线语音业务  无线数据业务  无线网络结构  基站退服  宽带  魔盒
    if ([dimName isEqualToString:@"业务发展"]) {
        
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xfffa796f) endColor:UICOLOR_HEX(0xfff2a356) frame:headV.colorBaseBtn.bounds] atIndex:0];
        
    }else if ([dimName isEqualToString:@"无线接入"]) {
        
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xff2895f6) endColor:UICOLOR_HEX(0xff6341f9) frame:headV.colorBaseBtn.bounds] atIndex:0];
        
    }else if ([dimName isEqualToString:@"无线覆盖"]) {
        
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xff4e89fb) endColor:UICOLOR_HEX(0xff896af8) frame:headV.colorBaseBtn.bounds] atIndex:0];
        
    }else if ([dimName isEqualToString:@"无限语音业务"]) {
        
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xff2cb6ac) endColor:UICOLOR_HEX(0xff63e09d) frame:headV.colorBaseBtn.bounds] atIndex:0];
        
    }else if ([dimName isEqualToString:@"无线数据业务"]) {
        
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xff167ffb) endColor:UICOLOR_HEX(0xff20befc) frame:headV.colorBaseBtn.bounds] atIndex:0];
        
    }else if ([dimName isEqualToString:@"无线网络结构"]) {
        
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xffd670b8) endColor:UICOLOR_HEX(0xffa663d4) frame:headV.colorBaseBtn.bounds] atIndex:0];
        
    }else if ([dimName isEqualToString:@"基站退服"]) {
        
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xfff16a81) endColor:UICOLOR_HEX(0xffe270ab) frame:headV.colorBaseBtn.bounds] atIndex:0];
        
    }else if ([dimName isEqualToString:@"宽带业务"]) {
        
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xfff4a24f) endColor:UICOLOR_HEX(0xfffc7e6a) frame:headV.colorBaseBtn.bounds] atIndex:0];
        
    }else if ([dimName isEqualToString:@"传输网络"]) {
        
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xfffc7e6a) endColor:UICOLOR_HEX(0xfff4a24f) frame:headV.colorBaseBtn.bounds] atIndex:0];
        
    }else if ([dimName isEqualToString:@"魔百盒业务"]) {
        
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xff60e39a) endColor:UICOLOR_HEX(0xff25bdab) frame:headV.colorBaseBtn.bounds] atIndex:0];
        
    }else {
        
        // 实时数据
        [headV.colorBaseBtn.layer insertSublayer:[ProjectTools setModuleListJianBianWithBenginColor:UICOLOR_HEX(0xff169bfa) endColor:UICOLOR_HEX(0xff9976e5) frame:headV.colorBaseBtn.bounds] atIndex:0];
    }
    
    return headV;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dic = _data[indexPath.section];
    NSDictionary *resultDic = [dic[@"list"] objectAtIndex:indexPath.row];
        
    if ([_cityName isEqualToString:@"全省"]) {
        
        ModuleDetailVC *detailVC = [[ModuleDetailVC alloc] init];
        detailVC.moduleName = resultDic[@"inD_Name"];
        detailVC.s_id = resultDic[@"s_id"];
        detailVC.cityName = _cityName;
        [self.navigationController pushViewController:detailVC animated:YES];
        
    }else {
        
        CityDetailVC *vc = [[CityDetailVC alloc] init];
        vc.moduleName = resultDic[@"inD_Name"];
        vc.cityName = _cityName;
        vc.s_id = resultDic[@"s_id"];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - tableView dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return [_data count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [_data[section][@"list"] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifierA = @"collect_list_cell";
    
    CollectCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierA];
    
    if (cell == nil) {
        
        cell = [[CollectCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifierA];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        cell.contentView.backgroundColor = [UIColor clearColor];
    }
    
    [cell reloadSubViewsWithData:_data indexPath:indexPath];
    
    return cell;
}

#pragma mark - 进入维度列表页
- (void)sectionModuleClick:(UIButton *)btn {
    
    NSInteger tag = btn.tag - 999;
    
    NSDictionary *dic = _data[tag];
    
    ModuleListVC *listVC = [[ModuleListVC alloc] init];
    listVC.moduleName = [NSString stringWithFormat:@"%@", dic[@"dim_name"]];
    listVC.sdim_ID = [NSString stringWithFormat:@"%@", dic[@"dimid"]];
    listVC.cityName = _cityName;
    listVC.type = @"3";
    listVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:listVC animated:YES];
    
}


@end
